<?php
namespace App\Services\H5;

use Illuminate\Support\Facades\DB;

class TmpService extends BaseService
{
    /**
     * 业务场景
     */

    /**
     * 获取一条记录
     * @param string $code 码
     * @param int $uid 用户ID
     * @return array
     */
    public function getOneByCode($code, $h5code)
    {
        //有没有TMP
        $tmpOne = (array) $this->dbConn($this->dbTmp)
            ->where('del', 1)
            ->where('code', $code)
            ->orderBy('id', 'desc')
            ->first();
        if (empty($tmpOne)) {
            return $this->toback(-2, 'no data');
        }
        $tmpOne['isfollow'] = 0;
        $tmpOne['attrList'] = [];
        //场景
        $sceneOne = (array) $this->dbConn($this->dbScene)
            ->where('id', $tmpOne['scene_id'])
            ->first();
        $tmpOne['scene_name'] = $sceneOne['title'];
        //访问量
        $codeOne = (array) $this->dbConn($this->dbQr)
            ->where('code', $tmpOne['code'])
            ->first();
        $tmpOne['show_code'] = $codeOne['show_code'];
        $tmpOne['visit_total'] = $this->dbConn($this->dbQrlog)
            ->where('code', $code)
            ->count('id');
        //有没有关注
        $followOne = (array) $this->dbConn($this->dbTmpFollow)
            ->where('del', 1)
            ->where('tmp_id', $tmpOne['id'])
            ->where('h5code', $h5code)
            ->first();
        if (!empty($followOne)) {
            $tmpOne['isfollow'] = 1;
        }
        //有没有属性
        $attrOnes = $this->dbConn($this->dbTmpAttr)
            ->select('id as attr_id', 'tmp_id as tmp_id', 'attr_name as attr_key', 'val as attr_val')
            ->where('del', 1)
            ->where('tmp_id', $tmpOne['id'])
            ->get();
        if (!empty($attrOnes)) {
            $tmpOne['attrList'] = $attrOnes;
        }
        //获取广告
        $tmpOne['ad'] = $this->getAdOne();
        return $this->toback(0, '', $tmpOne);
    }

    /**
     * 获取评论列表
     * @param string $h5code 用户code
     * @return array
     */
    public function replyList($code)
    {
        $tmpOne = (array) $this->dbConn($this->dbTmp)
            ->where('del', 1)
            ->where('code', $code)
            ->skip(0)->take(5)
            ->orderBy('id', 'desc')
            ->first();
        if (empty($tmpOne)) {
            return $this->toback(-2, 'no data');
        }
        $res = $this->dbConn($this->dbTmpReply)
            ->where('del', 1)
            ->where('tmp_id', $tmpOne['id'])
            ->skip(0)->take(5)
            ->orderBy('id', 'desc')
            ->get();
        if (!count($res)) {
            return $this->toback(-2, 'no data');
        }
        $arys = [];
        foreach ($res as $v) {
            $v = (array) $v;
            $strPrefix = substr($v['h5code'], 0, 2);
            $strSufffix = substr($v['h5code'], -2);
            $arys[] = [
                'id' => $v['id'],
                'pub' => $strPrefix.'...'.$strSufffix,
                'cnt' => $v['cnt'],
                'addtime' => $v['addtime'],
            ];
        }
        return $this->toback(0, '', $arys);
    }

    /**
     * 评论新增
     * @param string $code 码
     * @param string $h5code 用户编号
     * @param string $cnt 评论内容
     * @return array
     */
    public function replyAdd($code, $h5code, $cnt)
    {
        //获取模板
        $tmpOne = (array) $this->dbConn($this->dbTmp)
            ->where('del', 1)
            ->where('code', $code)
            ->orderBy('id', 'desc')
            ->first();
        if (empty($tmpOne)) {
            return $this->toback(-2, 'no data');
        }
        //插入评论
        $pas = [
            'h5code' => $h5code,
            'tmp_id' => $tmpOne['id'],
            'cnt' => $cnt,
            'addtime' => toDate(),
        ];
        $lastId = $this->dbConn($this->dbTmpReply)->insertGetId($pas);
        if (!$lastId) {
            return $this->toback(-2, 'error');
        }
        return $this->toback();
//        try {
//        } catch (\Exception $e) {
//            echo $e->getMessage();
//            return $this->toback(-1, 'error');
//        }
    }

    /**
     * 关注列表
     * @param string $code 码
     * @return array
     */
    public function getFollowList($code)
    {
        $tmpOne = (array) $this->dbConn($this->dbTmp)
            ->where('del', 1)
            ->where('code', $code)
            ->orderBy('id', 'desc')
            ->first();
        if (empty($tmpOne)) {
            return array('code' => '-2', 'msg' => '没有物品');
        }
        $arys = $this->dbConn($this->dbTmpFollow)
            ->where('del', 1)
            ->where('tmp_code', $tmpOne['tmp_code'])
            ->get();
        if (!count($arys)) {
            return array('code' => '-2', 'msg' => '没有关注');
        }
        return array('code' => 0, 'msg' => 'success', 'data' => $arys);
    }

    /**
     * 关注设置
     * @param string $code 码
     * @param string $h5code 用户编号
     */
    public function setFollow($code, $h5code)
    {
        $tmpOne = (array) $this->dbConn($this->dbTmp)
            ->where('del', 1)
            ->where('code', $code)
            ->orderBy('id', 'desc')
            ->first();
        if (empty($tmpOne)) {
            return $this->toback(-2, '没有记录');
        }
        $res = (array) $this->dbConn($this->dbTmpFollow)
            ->where('del', 1)
            ->where('tmp_id', $tmpOne['id'])
            ->where('h5code', $h5code)
            ->first();
        if (!empty($res)) {
            return $this->toback(-2, '已关注');
        }
        $fData = [
            'tmp_id' => $tmpOne['id'],
            'h5code' => $h5code,
            'addtime' => toDate(),
        ];
        $lastid = $this->dbConn($this->dbTmpFollow)->insertGetId($fData);
        if (!$lastid) {
            return $this->toback(-1, 'error');
        }
        return $this->toback();
    }

    /**
     * 广告使用
     * @param int $id 广告ID
     * @param int $use_id
     * @param string $h5code 用户编号
     * @param string $code 码值
     */
    public function setAdUse($ad_id, $use_id, $h5code, $code)
    {
        $tmpOne = (array) $this->dbConn($this->dbTmp)
            ->where('code', $code)
            ->first();
        if (empty($tmpOne)) return false;
        $adOne = (array) $this->dbConn($this->dbAdList)
            ->where('id', $ad_id)
            ->first();
        if (empty($adOne)) return false;
        if (!$adOne['num']) return false;
        $this->dbConn($this->dbAdList)
            ->where('id', $ad_id)
            ->update(['num' => $adOne['num'] - 1, 'uptime' => toDate()]);
        $this->dbConn($this->dbAdUsed)
            ->where('id', $use_id)
            ->update([
                'plat_uid' => $tmpOne['uid'],
                'h5code' => $h5code, 'usetime' => toDate(),
            ]);
        return true;
    }
}